import useConfig from '@/common/config.js'
const request = (opts, data) => {


	// 监听网络状态
	uni.onNetworkStatusChange(function(res) {
		if (!res.isConnected) {
			uni.showToast({
				title: '网络连接不可用！',
				icon: 'none'
			});
		}
		return false
	});
	const header = opts.method == 'get' ? {
		'X-Requested-With': 'XMLHttpRequest',
		"Accept": "application/json",
		"Content-Type": "application/json; charset=UTF-8"
	} : {
		'X-Requested-With': 'XMLHttpRequest',
		'Content-Type': 'application/json; charset=UTF-8'
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: useConfig.baseURL + opts.url,
			data: data,
			method: opts.method,
			header,
			success: (res) => {
				const {
					data
				} = res
				resolve(data)
			},
			fail: (error) => {
				reject(error)
			}
		})
	})

};

export default request